<?php


namespace App\Http\Repositories\Services\Impls;


use App\Exceptions\ServiceException;
use App\Http\Constants\CommonExceptionConstants;
use App\Http\Constants\ServiceExceptionConstants;
use App\Http\Repositories\Daos\IBrandDao;
use App\Http\Repositories\Eloquent\Brand;
use App\Http\Repositories\Services\IBrand;
use App\Http\Requests\Admin\BrandInputDTO;
use App\Util\Helper;

class BrandService implements IBrand
{
    protected $brandDao;

    public function __construct(IBrandDao $brandDao)
    {
        $this->brandDao = $brandDao;
    }

    public function byId($id)
    {
        $dao = $this->brandDao->find($id);
        if (!$dao) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }
        return $dao;
    }

    public function getTree($pid = 0, $data = []){
        $search = [];
        $search['parent_id'] = $pid;
        $dao = $this->brandDao->getAll($search);
        if ($dao->count()) {
            $arr = [];
            foreach ($dao as $key=>$val){
                $arr[$key]['id'] = $val->id;
                if ($val->first) {
                    $arr[$key]['label'] = $val->first.' - '.$val->name;
                } else {
                    $arr[$key]['label'] = $val->name;
                }
                $arr[$key]['children'] = $this->getTree($val->id, $data);
            }
            $data = $arr;
        }
        return $data;
    }

    public function getAll($parentId = '')
    {
        $dao = $this->brandDao->getAll([])->toArray();
        $data = [];
        if ($dao) {
            Helper::rLevel('id',$dao,$data);
        }
        $dao = [];
        $i = 0 ;
        foreach ($data as $key=>$val) {
            $val['name'] = $val['first'] ? $val['first'].'-'.$val['name'] : $val['name'];
            $dao[$i] = $val;
            $i++;
        }
        return $dao;
    }

    public function getParentId($id)
    {
        $search = [];
        $search['parent_id'] = $id;
        return $this->brandDao->getAll($search)->toArray();
    }

    public function store(BrandInputDTO $inputDTO)
    {
        $dao = new Brand();
        $dao->name = trim($inputDTO->get('name'));
        $dao->sort = intval($inputDTO->get('sort'));
        $dao->parent_id = intval($inputDTO->get('parent_id'));
        $dao->first = '';
        if (!$dao->parent_id) {
            $dao->first = $this->_getFirst($dao->name);
        }
        $dao->save();
    }

    public function update(BrandInputDTO $inputDTO, $id)
    {
        $dao = $this->brandDao->find($id);
        if (!$dao) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }
        $dao->name = trim($inputDTO->get('name'));
        $dao->sort = intval($inputDTO->get('sort'));
        $dao->parent_id = intval($inputDTO->get('parent_id'));
        $dao->first = '';
        if (!$dao->parent_id) {
            $dao->first = $this->_getFirst($dao->name);
        }
        $dao->save();
    }

    public function delete($id)
    {
        $dao = $this->brandDao->find($id);
        if (!$dao) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }
        $exists = $this->brandDao->exists([
           'parent_id' => $id,
           'is_deleted' => 0
        ]);
        if ($exists) {
            throw new ServiceException(ServiceExceptionConstants::getKey('is_children'));
        }
        $dao->is_deleted = 1;
        $dao->save();
    }

    private function _getFirst($str)
    {
        return strtoupper(pinyin_abbr(mb_substr($str,0,1)));
    }
}